package com.lovetemperature.action;

import java.util.Date;
import java.util.List;
import com.lovetemperature.model.GoodCategory;
import com.lovetemperature.model.Shop;
import com.lovetemperature.model.Stock;
import com.lovetemperature.service.GoodService;
import com.lovetemperature.service.ShopService;
import com.lovetemperature.service.StockService;
import com.opensymphony.xwork2.ActionSupport;

public class OneGoodAction extends ActionSupport {
	private GoodService goodService;
	private ShopService shopService;
	private StockService stockService;
	
	private String stockId;
	private Stock stock;
	
	private List<Shop> candidateShops;
	private GoodCategory[] candidateCategories = GoodCategory.values();
	
	private String shopId;
	private String listDate;
	
	private boolean newAdd;
	
	public String show(){
		candidateShops = shopService.findAllShop();
		if(stockId==null || stockId.equalsIgnoreCase("")){ //新增产品
			stock = new Stock();
			newAdd = true;
		}else{	//修改已有的产品
			stock = stockService.findById(Long.parseLong(stockId));
			newAdd = false;
		}
		stockId = null;
		return "success";
	}
	
	public String submit() {
		if(stockId==null || stockId.equalsIgnoreCase("")){ //新增产品
			finishStock();
			stockService.addStock(stock);
		}else{	//修改已有的产品
			finishStock();
			stockService.update(stock);
		}
		stockId = null;
		return "success";
	}
	
	public String delect(){
		if(stockId!=null && !stockId.equalsIgnoreCase("")){
			stock = stockService.findById(Long.parseLong(stockId));
			stockService.delect(stock);
		}
		return "success";
	}

	private void finishStock() {
		Shop tmp = shopService.findById(Long.parseLong(shopId));
		stock.setShop(tmp);
		if(listDate!=null && !listDate.equalsIgnoreCase("")){
			stock.setListDate(new Date(listDate));
		}
	}

	public GoodService getGoodService() {
		return goodService;
	}

	public void setGoodService(GoodService goodService) {
		this.goodService = goodService;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public List<Shop> getCandidateShops() {
		return candidateShops;
	}

	public void setCandidateShops(List<Shop> candidateShops) {
		this.candidateShops = candidateShops;
	}

	public GoodCategory[] getCandidateCategories() {
		return candidateCategories;
	}

	public void setCandidateCategories(GoodCategory[] candidateCategories) {
		this.candidateCategories = candidateCategories;
	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public Stock getStock() {
		return stock;
	}

	public void setStock(Stock stock) {
		this.stock = stock;
	}

	public String getListDate() {
		return listDate;
	}

	public void setListDate(String listDate) {
		this.listDate = listDate;
	}

	public String getStockId() {
		return stockId;
	}

	public void setStockId(String stockId) {
		this.stockId = stockId;
	}

	public boolean isNewAdd() {
		return newAdd;
	}

	public void setNewAdd(boolean newAdd) {
		this.newAdd = newAdd;
	}
}
